chrome.storage.sync.get(['sohu'], function(result) {
    console.log('sohu.js: Value currently is ' + result.sohu);
    if (result.sohu) sohuSimple();
});

function hideAllOtherSibes(main) {
    let children = main.children;
    for (let i=1; i<children.length; i++) {
        children[i].className += " iread_sohu_hide";
    }
}

function restoreAllOtherSibes(main) {
    let children = main.children;
    for (let i=1; i<children.length; i++) {
        children[i].className =  children[i].className.replace("iread_sohu_hide", "");
    }
}

///////////Entry//////////////
function sohuSimple() {
    if (!document.URL.includes("sohu.com")) {
        return;
    }
    insertCss('sohu.css');
    let {ful_scr_toolbar,ful_scr_toolbar_close} = inject_iReaderBar()

    ful_scr_toolbar_close.onclick= onReadBtnClick;

    let main_css_class = "left main"
    let main = document.getElementsByClassName(main_css_class)[0];
    let content_css = 'iread_content';

    let iread_text_css = 'iread_sohu_text'
    let bodyLastBgColor = main?main.style.background:"";

    if (!main) {
        main_css_class = "content area"
        main = document.getElementsByClassName("content area")[0];
        content_css = 'iread_content_ex';
        iread_text_css = 'iread_sohu_text_ex';
    }

    let iread_content = main.children[0];
    let iread_text = iread_content.children[0];


    function enterFullScreen(){
        ful_scr_toolbar.className = "ful_scr_toolbar"
        ful_scr_toolbar_close.textContent = "×";
    
        if (main) {
            main.className = "iread_main";
            hideAllOtherSibes(main);
        }
 
        if (iread_text) iread_text.className += " " + iread_text_css;
    
        chrome.storage.sync.get(['bgcolor'], function(result) {
            let bgcolor = result.bgcolor?result.bgcolor:"rgb(227, 227, 227)";
            if(main) main.setAttribute('style', "background:"+bgcolor+" !important");
        });
    
        if (iread_content) {
            iread_content.className += " " + content_css;
        }

        showDonate(true);
    }
    
    function exitFullScreen(){
        ful_scr_toolbar.className = "ful_scr_toolbar_notful";
        ful_scr_toolbar_close.textContent = "+";
    
        if (main) {
            main.className = main_css_class;
            main.style.background = bodyLastBgColor;
            restoreAllOtherSibes(main);
        }
        if (iread_content) iread_content.className = iread_content.className.replace(content_css, "");
        if (iread_text) iread_text.className = iread_text.className.replace(iread_text_css, "");

        showDonate(false);
    }

    function onReadBtnClick(){
        if (main && main.className.includes("iread_main")) {
            exitFullScreen();
        } else {
            enterFullScreen();
        }
    }

    chrome.storage.sync.get(['i_sohu'], function(result) {
        console.log('sohu.js: auto is ' + result.i_sohu);
        if (result.i_sohu) enterFullScreen();
    });
}



